package com.huawei.hwid.core.d;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.XmlResourceParser;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Build.VERSION;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import com.huawei.android.os.BuildEx;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.c.a;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.encrypt.f;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.hwid.d;
import com.huawei.hwid.usecase.GetUserInfoUseCase;
import com.huawei.hwid.usecase.GetUserInfoUseCase.RequestValues;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.xmlpull.v1.XmlPullParserException;

public class b {
    public static boolean a(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        try {
            byte[] bytes = str.getBytes("UTF-8");
            for (byte b : bytes) {
                int i = b & 255;
                if (i <= 31 || i >= 127) {
                    e.b("BaseUtil", "byte not printable", true);
                    return false;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.b("BaseUtil", "UnsupportedEncodingException", true);
        }
        return true;
    }

    public static String a(byte[] bArr) {
        if (bArr == null) {
            return null;
        }
        if (bArr.length == 0) {
            return "";
        }
        char[] cArr = new char[(bArr.length * 2)];
        char[] cArr2 = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        for (int i = 0; i < bArr.length; i++) {
            byte b = bArr[i];
            cArr[i * 2] = cArr2[(b & 240) >> 4];
            cArr[(i * 2) + 1] = cArr2[b & 15];
        }
        return new String(cArr);
    }

    public static byte[] b(String str) {
        byte[] bArr = new byte[(str.length() / 2)];
        try {
            byte[] bytes = str.getBytes("UTF-8");
            for (int i = 0; i < bArr.length; i++) {
                bArr[i] = (byte) (((byte) (Byte.decode("0x" + new String(new byte[]{bytes[i * 2]}, "UTF-8")).byteValue() << 4)) ^ Byte.decode("0x" + new String(new byte[]{bytes[(i * 2) + 1]}, "UTF-8")).byteValue());
            }
        } catch (UnsupportedEncodingException e) {
            e.d("BaseUtil", "hexString2ByteArray UnsupportedEncodingException", true);
        }
        return bArr;
    }

    public static boolean a(Context context, int i) {
        int a;
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
        if (com.huawei.hwid.core.d.c.b.b()) {
            a a2 = com.huawei.hwid.core.d.c.b.a();
            if (i == HwAccountConstants.NO_SUBID) {
                i = a2.a();
            }
            a = a2.a(i);
        } else if (telephonyManager != null) {
            a = telephonyManager.getSimState();
        } else {
            a = -1;
        }
        if (a == 5) {
            return true;
        }
        return false;
    }

    public static boolean a(Context context) {
        e.b("BaseUtil", "enter networkIsAvaiable", true);
        if (context == null) {
            return false;
        }
        Object systemService = context.getSystemService("connectivity");
        if (systemService == null) {
            e.b("BaseUtil", "connectivity is null,so networkIsAvaiable is unaviable", true);
            return false;
        }
        NetworkInfo[] allNetworkInfo = ((ConnectivityManager) systemService).getAllNetworkInfo();
        if (allNetworkInfo == null || allNetworkInfo.length == 0) {
            e.b("BaseUtil", "NetworkInfo is null,so networkIsAvaiable is unaviable", true);
            return false;
        }
        int i = 0;
        while (i < allNetworkInfo.length) {
            if (allNetworkInfo[i] != null && allNetworkInfo[i].getState() == State.CONNECTED) {
                return true;
            }
            i++;
        }
        e.b("BaseUtil", "NetworkInfo  state is unaviable", true);
        return false;
    }

    public static String b(Context context) {
        String[] strArr = new String[]{"Unknown", "Unknown"};
        if (context.getPackageManager().checkPermission("android.permission.ACCESS_NETWORK_STATE", context.getPackageName()) != 0) {
            strArr[0] = "Unknown";
            return strArr[0];
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            strArr[0] = "Unknown";
            return strArr[0];
        }
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(1);
        if (networkInfo == null || networkInfo.getState() != State.CONNECTED) {
            NetworkInfo networkInfo2 = connectivityManager.getNetworkInfo(0);
            if (networkInfo2 == null || networkInfo2.getState() != State.CONNECTED) {
                return strArr[0];
            }
            strArr[0] = "2G/3G/4G";
            strArr[1] = networkInfo2.getSubtypeName();
            return strArr[0] + strArr[1];
        }
        strArr[0] = "Wi-Fi";
        return strArr[0];
    }

    public static String a() {
        return g("yyyyMMddHHmmssSSS");
    }

    @SuppressLint({"SimpleDateFormat"})
    public static String a(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) {
            return "";
        }
        try {
            return new SimpleDateFormat(str3).format(new SimpleDateFormat(str2).parse(str));
        } catch (RuntimeException e) {
            e.b("BaseUtil", "RuntimeException", true);
            return "";
        } catch (Exception e2) {
            e.b("BaseUtil", "Exception", true);
            return "";
        }
    }

    public static String c(Context context) {
        if (context != null) {
            return context.getPackageName();
        }
        return "";
    }

    public static String d(Context context) {
        return context.getResources().getConfiguration().locale.getLanguage().toLowerCase(Locale.getDefault());
    }

    public static String e(Context context) {
        return context.getResources().getConfiguration().locale.getCountry().toLowerCase(Locale.getDefault());
    }

    private static String o(Context context) {
        Configuration configuration = context.getResources().getConfiguration();
        if (VERSION.SDK_INT >= 21) {
            return configuration.locale.getScript();
        }
        return null;
    }

    public static String f(Context context) {
        if (context == null) {
            return "";
        }
        if (TextUtils.isEmpty(o(context))) {
            return d(context) + "-" + e(context).toUpperCase(Locale.getDefault());
        }
        return d(context) + "-" + o(context) + "-" + e(context).toUpperCase(Locale.getDefault());
    }

    public static String c(String str) {
        if (str == null) {
            return "0";
        }
        if (str.startsWith("+")) {
            str = str.replace("+", "00");
        }
        String str2 = "0";
        if (str.contains("@")) {
            str2 = "1";
        }
        if (m.c(str)) {
            return "2";
        }
        return str2;
    }

    public static int a(Context context, String str) {
        HwAccount b = com.huawei.hwid.d.a.a(context).b();
        if (b != null) {
            return b.e();
        }
        return 0;
    }

    public static String a(String str, String str2) {
        StringBuffer stringBuffer = new StringBuffer();
        if (!(TextUtils.isEmpty(str) || TextUtils.isEmpty(str2))) {
            if ("com.huawei.hwid".equals(str2)) {
                stringBuffer.append(str);
            } else if (str.length() >= 20) {
                String substring = str.substring(0, 20);
                stringBuffer.append(substring).append(f.a(str + ":" + str2));
            }
        }
        return stringBuffer.toString();
    }

    public static boolean a(HwAccount hwAccount) {
        if (hwAccount == null) {
            return false;
        }
        if (!TextUtils.isEmpty(hwAccount.b()) && !TextUtils.isEmpty(hwAccount.d()) && !TextUtils.isEmpty(hwAccount.c()) && !TextUtils.isEmpty(hwAccount.g())) {
            return true;
        }
        e.a("BaseUtil", "addHwAccount is invalid", true);
        return false;
    }

    public static boolean g(Context context) {
        return context.getPackageName().equals("com.huawei.hwid");
    }

    public static boolean a(Context context, String str, boolean z) {
        Object b = g.b(context, str);
        if (!TextUtils.isEmpty(b)) {
            try {
                z = Boolean.parseBoolean(b);
            } catch (Exception e) {
            }
        }
        return z;
    }

    public static boolean b(Context context, String str) {
        return a(context, str, "com.huawei.hwid");
    }

    public static boolean a(Context context, String str, String str2) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(str);
        intent.setPackage(str2);
        List list = null;
        if (packageManager != null) {
            list = packageManager.queryIntentActivities(intent, 0);
        }
        if (list == null || list.isEmpty()) {
            return false;
        }
        return true;
    }

    public static boolean c(Context context, String str) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(str);
        intent.setPackage("com.huawei.hwid");
        List list = null;
        if (packageManager != null) {
            list = packageManager.queryIntentServices(intent, 0);
        }
        if (list == null || list.isEmpty()) {
            return false;
        }
        return true;
    }

    public static byte[] d(String str) {
        if (TextUtils.isEmpty(str)) {
            e.d("BaseUtil", "getUTF8Bytes, str is empty", true);
            return new byte[0];
        }
        try {
            return str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.d("BaseUtil", "getBytes error", true);
            return new byte[0];
        }
    }

    public static boolean h(Context context) {
        if (context == null) {
            return true;
        }
        List list = null;
        com.huawei.hwid.a a = com.huawei.hwid.a.a();
        if (a.b() == null) {
            try {
                list = i(context);
                a.a(list);
            } catch (Exception e) {
                e.d("BaseUtil", "Exception", true);
            }
        } else {
            list = a.b();
        }
        String packageName = context.getPackageName();
        if (list == null || !list.contains(packageName)) {
            return true;
        }
        return false;
    }

    public static List<String> i(Context context) {
        String str = HiAnalyticsConstant.BI_KEY_PACKAGE;
        List arrayList = new ArrayList();
        XmlResourceParser xml = context.getResources().getXml(l.b(context, "usesdk_packagename"));
        if (xml != null) {
            try {
                for (int eventType = xml.getEventType(); 1 != eventType; eventType = xml.next()) {
                    String name = xml.getName();
                    if (eventType == 2 && str.equals(name)) {
                        arrayList.add(xml.nextText());
                    }
                }
            } catch (XmlPullParserException e) {
                e.d("BaseUtil", "Parser xml exception: XmlPullParserException", true);
            } catch (IOException e2) {
                e.d("BaseUtil", "Parser xml exception: IOException", true);
            }
        }
        return arrayList;
    }

    public static boolean j(Context context) {
        try {
            if (context.getPackageManager().getApplicationInfo("com.huawei.hwid", DnsConfig.MAX_CACHE_ENTRIES) != null) {
                return true;
            }
            return false;
        } catch (NameNotFoundException e) {
            e.b("BaseUtil", "NameNotFoundException", true);
            return false;
        }
    }

    public static String k(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            e.d("BaseUtil", "NameNotFoundException getVersionTag error", true);
            return "";
        }
    }

    public static void a(Context context, Intent intent, int i) {
        if (context == null || intent == null) {
            e.d("BaseUtil", "context or intent is null.", true);
            return;
        }
        if (!(context instanceof Activity)) {
            intent.setFlags((268435456 | i) | HwAccountConstants.FLAG_TRANSLUCENT_STATUS);
            e.e("BaseUtil", "not send Activity", true);
        } else if (i != 0) {
            intent.setFlags(i);
        }
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.d("BaseUtil", "can not start activity Exception", true);
        }
    }

    public static boolean a(PackageManager packageManager, String str, String str2) {
        return packageManager.checkPermission(str, str2) == 0;
    }

    public static boolean e(String str) {
        if (TextUtils.isEmpty(str) || str.equals("0") || str.equals("1") || str.equals("2")) {
            return false;
        }
        return true;
    }

    public static HwAccount d(Context context, String str) {
        if (context == null) {
            e.b("BaseUtil", "context is null", true);
            return null;
        } else if (TextUtils.isEmpty(str)) {
            e.e("BaseUtil", "get account by userID failed, the userID is null!", true);
            return null;
        } else {
            List<HwAccount> a = com.huawei.hwid.c.a.a(context).a(context, l(context));
            if (!(a == null || a.isEmpty())) {
                for (HwAccount hwAccount : a) {
                    if (hwAccount != null && str.equalsIgnoreCase(hwAccount.d())) {
                        if (!b(hwAccount)) {
                            return null;
                        }
                        e.b("BaseUtil", "get account by userID success!", true);
                        return hwAccount;
                    }
                }
            }
            e.b("BaseUtil", "get account by userID failed, there is no matching account!", true);
            return null;
        }
    }

    public static boolean b(HwAccount hwAccount) {
        if (hwAccount != null && a(hwAccount.g())) {
            return true;
        }
        return false;
    }

    public static String l(Context context) {
        String b = g.b(context, HwAccountConstants.TOKEN_TYPE);
        if (!TextUtils.isEmpty(b)) {
            return b;
        }
        b = c(context);
        g.a(context, HwAccountConstants.TOKEN_TYPE, b);
        return b;
    }

    public static String m(Context context) {
        if (context == null) {
            return "";
        }
        HwAccount hwAccount;
        List a = com.huawei.hwid.c.a.a(context).a(context, l(context));
        if (a == null || a.size() <= 0) {
            hwAccount = null;
        } else {
            hwAccount = (HwAccount) a.get(0);
        }
        return hwAccount != null ? hwAccount.b() : null;
    }

    public static void a(Context context, String str, String str2, CloudRequestHandler cloudRequestHandler) {
        e.b("BaseUtil", "do getUserInfoReq in BaseUtil", true);
        if (context == null || cloudRequestHandler == null) {
            e.b("BaseUtil", "context or requestHandler is null", true);
        } else if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            cloudRequestHandler.onError(new ErrorStatus(1002, "userId  or queryRangeFlag is null"));
        } else {
            HwAccount d = d(context, str);
            if (d == null) {
                cloudRequestHandler.onError(new ErrorStatus(13, "no account by userId"));
            } else if (a(context, "getUserInfo", 30000)) {
                new com.huawei.hwid.b(d.a()).a(new GetUserInfoUseCase(context, d, cloudRequestHandler), new RequestValues(str2), null);
            } else {
                cloudRequestHandler.onError(new ErrorStatus(25, "Too many recent requests have been made and last request hasn't callback"));
            }
        }
    }

    public static boolean a(Context context, String str, int i) {
        if ("com.huawei.hwid".equals(context.getPackageName())) {
            return true;
        }
        long a = com.huawei.hwid.a.a().a(str);
        long currentTimeMillis = System.currentTimeMillis();
        boolean b = com.huawei.hwid.a.a().b(str);
        int c = com.huawei.hwid.a.a().c(str);
        if (0 == a || 0 == currentTimeMillis || ((long) i) < currentTimeMillis - a) {
            a(str, currentTimeMillis, c);
            e.b("BaseUtil", "last time or current time is zero, interval time is enough", true);
            return true;
        } else if (b) {
            a(str, currentTimeMillis, c);
            e.b("BaseUtil", "request has call back", true);
            return true;
        } else if (c >= 5) {
            return false;
        } else {
            a(str, currentTimeMillis, c);
            e.b("BaseUtil", "request number ", true);
            return true;
        }
    }

    private static void a(String str, long j, int i) {
        if (0 != j) {
            com.huawei.hwid.a.a().a(str, j);
        }
        com.huawei.hwid.a.a().a(str, false);
        com.huawei.hwid.a.a().a(str, i + 1);
    }

    public static void f(String str) {
        int c = com.huawei.hwid.a.a().c(str);
        if (c > 0) {
            com.huawei.hwid.a.a().a(str, c - 1);
        }
        com.huawei.hwid.a.a().a(str, true);
    }

    public static String b() {
        String str = "";
        try {
            String str2;
            Object a = i.a("android.os.SystemProperties", "get", new Class[]{String.class, String.class}, new Object[]{"ro.build.version.emui", ""});
            if (a != null) {
                str2 = (String) a;
            } else {
                str2 = str;
            }
            return str2;
        } catch (RuntimeException e) {
            e.c("BaseUtil", "RuntimeException", true);
            return str;
        } catch (Exception e2) {
            e.c("BaseUtil", "Exception", true);
            return str;
        }
    }

    public static String g(String str) {
        return new SimpleDateFormat(str, Locale.ENGLISH).format(new Date(System.currentTimeMillis()));
    }

    public static boolean h(String str) {
        try {
            Class.forName(str);
            return true;
        } catch (ClassNotFoundException e) {
            e.d("BaseUtil", "The class is not existing: ", true);
            return false;
        }
    }

    public static String c() {
        return "B778D57C1D7C80E09C1FFDD68A2BCF74";
    }

    public static int d() {
        try {
            Object a = i.a("android.os.UserHandle", "myUserId", null, null);
            if (a != null) {
                return ((Integer) a).intValue();
            }
        } catch (Exception e) {
            e.c("BaseUtil", "Exception", true);
        }
        return -1;
    }

    public static boolean e() {
        return d() == 0;
    }

    public static boolean f() {
        return VERSION.SDK_INT > 22;
    }

    public static boolean g() {
        if (!h("com.huawei.android.os.BuildEx") || BuildEx.VERSION.EMUI_SDK_INT < 9) {
            return false;
        }
        return true;
    }

    public static void a(Activity activity, boolean z) {
        if (VERSION.SDK_INT > 18) {
            Window window = activity.getWindow();
            LayoutParams attributes = window.getAttributes();
            if (z) {
                attributes.flags |= HwAccountConstants.FLAG_TRANSLUCENT_STATUS;
            } else {
                attributes.flags &= -67108865;
            }
            window.setAttributes(attributes);
        }
    }

    public static boolean a(Activity activity, Boolean bool) {
        try {
            Window window = activity.getWindow();
            e.b("BaseUtil", "setHwFloating", true);
            i.a(window.getClass(), window, "setHwFloating", new Class[]{Boolean.TYPE}, new Object[]{bool});
            return true;
        } catch (Exception e) {
            e.d("BaseUtil", "Exception", true);
            return false;
        }
    }

    public static String n(Context context) {
        String str = "";
        if ("com.huawei.hwid".equals(context.getPackageName())) {
            return "HwID_" + k(context);
        }
        return "HwID_SDK_" + HwAccountConstants.SDK_VERSION;
    }
}
